package org.example.一对多.dao;

import org.example.一对多.bean.EmpInfo;
import org.example.面向对象.db.DBManager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;

public class EmpInfoDAO {
    private Connection conn;
    private PreparedStatement ps;
    private ResultSet rs;

    public Set<EmpInfo> findEmpByDeptNo(int deptNo){
        Set<EmpInfo> set = new HashSet<>();
        String sql = "select *from emp where deptno=? and empstate=1";
        conn = DBManager.getConnection();
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1,deptNo);
            rs = ps.executeQuery();
            while (rs.next()){
                EmpInfo ei = new EmpInfo();
                ei.setEmpNo(rs.getInt(1));
                ei.setEname(rs.getString(2));
                ei.setJob(rs.getString(3));
                ei.setMgr(rs.getInt(4));
                ei.setHireDate(rs.getString(5));
                ei.setSal(rs.getDouble(6));
                ei.setComm(rs.getDouble(7));
                ei.setDeptNo(rs.getInt(8));
                ei.setEmpState(1);
                set.add(ei);
            }
        }catch (SQLException e){
            System.out.println(e.getMessage());
        }
        return set;
    }
}
